package com.micangcaihang.micang.mvp.task.ui;

import android.os.Bundle;

import com.micangcaihang.common.base.activity.BaseActivity;
import com.micangcaihang.common.base.fragment.BaseDataBingFragment;
import com.micangcaihang.common.base.fragment.BaseFragment;
import com.micangcaihang.common.utils.ALog;
import com.micangcaihang.common.utils.KeyboardUtils;
import com.micangcaihang.common.widget.tablayout.listener.OnTabChangedListner;
import com.micangcaihang.common.widget.titlebar.TitleBar;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.databinding.ActivityDataExmaineBinding;
import com.micangcaihang.micang.mvp.task.contract.DDataExmaineContract;
import com.micangcaihang.micang.mvp.task.presenter.DDataExmainePresenter;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/8/14
 *     update : 2017/8/14
 *     desc   : MiCang
 *     version: 1.0
 * </pre>
 */
public class DataExmaineActivity extends BaseActivity<DDataExmainePresenter,
        ActivityDataExmaineBinding> implements DDataExmaineContract.View {
    private BaseDataBingFragment[] mFragments;
    private String id;

    /**
     * 获取资源文件布局
     *
     * @return 资源布局文件layout
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_data_exmaine;
    }

    @Override
    protected void beforeInitView(Bundle savedInstanceState) {
        super.beforeInitView(savedInstanceState);
        if (savedInstanceState == null) {
            mFragments = new BaseFragment[]{ServiceExmaineFragment.newInstance(id)};
            loadMultipleRootFragment(R.id.main_root, 0, mFragments);
        } else {
            // 这里库已经做了Fragment恢复,所有不需要额外的处理了, 不会出现重叠问题
            for (int i = 0; i < mFragments.length; i++) {
                ALog.e(TAG, "initFragments" + i);
                mFragments[i] = findFragment(mFragments[i].getClass());
            }
        }
    }

    /**
     * 获取传递的bundle数据
     *
     * @param extras
     */
    @Override
    protected void getBundleExtras(Bundle extras) {
        id = extras.getString("id");
    }

    /**
     * 初始化按钮监听
     */
    @Override
    protected void initListener() {
        mViewBinding.tab.setOnTabChangedListner(new OnTabChangedListner() {
            @Override
            public void onTabSelected(int tabNum) {
                KeyboardUtils.hideSoftInput(DataExmaineActivity.this);
                int toDoHidden = -1;
                for (int i = 0; i < mFragments.length; i++) {
                    if (!mFragments[i].isHidden()) {
                        toDoHidden = i;
                    }
                }
                showHideFragment(mFragments[tabNum], mFragments[toDoHidden]);
            }
        });
        mViewBinding.titleBar.setDelegate(new TitleBar.Delegate() {
            @Override
            public void onClickLeftCtv() {
                finish();
            }

            @Override
            public void onClickTitleCtv() {

            }

            @Override
            public void onClickRightCtv() {

            }

            @Override
            public void onClickRightSecondaryCtv() {

            }
        });
    }
}
